/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.login;

import edu.uoc.tdp.pac4.comun.GestorInterface;
import edu.uoc.tdp.pac4.comun.GestorLoginInterface;
import edu.uoc.tdp.pac4.comun.Utils;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Locale;
import java.sql.SQLException;

//import edu.uoc.tdp.pac4.comun.GestorLoginInterface;
import edu.uoc.tdp.pac4.beans.Usuari;

/**
 *
 * @author Nurele
 */
public class login extends JFrame {
    
   JDesktopPane loginWindow;
   JLabel lbl_user;
   JLabel lbl_pass;
   JLabel lbl_msg;
   JTextField frm_user;
   JPasswordField frm_password;
   JButton btn_login;
    
    private GestorLoginInterface conexion;
    //private GestorInterface conexion;
    private final String URL = "localhost";
    private final String JNDI_NAME ="CATL";
    private final int PORT = 1099;
    
    private GestorLoginInterface if1;
  
   public login() {
           initComponents();
           conectarServerRMI();

   
           
   
   }
   
   
   
   
   
    

    private void seleccionDeIdioma() {
        Locale localLocale = new Locale("es", "ES");
        TDSLanguageUtils.setDefaultLanguage("i18n/messages");
    }
    
    public void initComponents() { 
    
        loginWindow=new JDesktopPane();
        loginWindow.setBackground(Color.gray);
        add(loginWindow);
        //this.setTitle(TDSLanguageUtils.getMessage("Login.title"));
        this.setTitle("Login"); // MODIF
        
        /* Etiquetes */
        
        lbl_user = new JLabel();
        lbl_user.setText("Usuario");
        lbl_user.setBounds(20,100,150,25);
        
        lbl_pass = new JLabel();
        lbl_pass.setText("Password");
        lbl_pass.setBounds(20,140,150,25);
        
        /* Si és error, la posem */
        
        lbl_msg = new JLabel();
        lbl_msg.setForeground(Color.red);
        lbl_msg.setBounds(20,40,300,25);        
        
        
        /* Camps de login i pass */
        
        frm_user = new JTextField();
        frm_user.setBounds(150,100,150,25);
        
        frm_password = new JPasswordField();
        frm_password.setBounds(150,140,150,25);
        
        /* Botó */
        
        btn_login = new JButton();
        btn_login.setText("Validarse");
        btn_login.setBounds(100,200,150,40);
        
        
        /* Dibuixem per pantalla */
        
        loginWindow.add(lbl_user);
        loginWindow.add(lbl_pass);
        loginWindow.add(frm_user);
        loginWindow.add(frm_password);
        loginWindow.add(btn_login);
        
        loginWindow.add(lbl_msg);
        
        
        /* Listener clic al botó de login */
        
        this.btn_login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)   {
                            System.out.println("Login!");

                            String username = frm_user.getText();
                            String password = new String(frm_password.getPassword());
                            
                                    
                            
                            if(username.isEmpty() || username.length()<5) {
                               lbl_msg.setText("El usuario debe contener almenos 5 carácteres");
                            } else if (password.isEmpty()) {
                               lbl_msg.setText("No ha tecleado ninguna contraseña");
                            } else { /* Enviem la petició */
                                try {
                                    
                                    int id = conexion.usuariLogin(username, password); 
                                    
                                   if( id > 0) {
                                       System.out.println("Login OK");
                                       
                                       /* Carregar l'objecte Usuari */
                                       
                                       Usuari usuari ;
                                       usuari = conexion.loadUsuari(id); 
                                       
                                       System.out.println("Mi login es : "+usuari.getUsuari());
                                       
                                   } else {
                                       System.out.println("Login Error");
                                       lbl_msg.setText("Usuario o contraseña incorrecta");
                                   }
                                } catch (RemoteException e) {
                                   e.printStackTrace();
                               } catch (SQLException e) {
                                   e.printStackTrace();
                               }
                            }
                            
                        };
        });
        
        
        
    }
    
    
    
    
    
    
        public void conectarServerRMI() {
        try {
            Registry registry = LocateRegistry.getRegistry(URL,PORT);
           // this.conexion = (GestorInterface)registry.lookup(JNDI_NAME);
            
            this.conexion = (GestorLoginInterface)registry.lookup(JNDI_NAME);
            System.out.println("Aca");
            
           
            
            
            System.out.println(this.conexion.saludo());
            
            
            //Usuario us;
            //us = conexion.userLogin();
            
            //System.out.println(us.getUsuari());
            
            
        } catch (NotBoundException e) {
           // Utils.shownError(TDSLanguageUtils.getMessage("Presupuesto.RMIError"));
            e.printStackTrace();
            System.exit(0);
        } catch (RemoteException e) {
            // Utils.shownError(TDSLanguageUtils.getMessage("Presupuesto.RMIError"));
            e.printStackTrace();
            System.exit(0);
        } catch (Exception e) {
            //Utils.shownError(TDSLanguageUtils.getMessage("Aplication.ExcpGeneral"));
            e.printStackTrace();
            System.exit(0);
        }
    }
    
    
    
    
    
    public static void main(String[] args){
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                login l = new login();
				l.setSize(400,300);
                //p.setExtendedState(MAXIMIZED_BOTH);
                l.setVisible(true);
                l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
            }
        });
        
        
    }
    
    
    
    
}
